<template>
  <div>
    <el-upload
      v-show="!isShowProgress"
      action="#"
      list-type="picture-card"
      :file-list="fileList"
      :class="{ display: fileList.length }"
      :on-remove="onRemove"
      :on-change="onChange"
      :http-request="onRequest"
      :on-preview="handlePictureCardPreview"
      :before-upload="beforeUpload"
      accept="image/png, image/jpeg, image/gif"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-progress v-if="isShowProgress" type="circle" :percentage="percentage" />
    <!-- sync -->
    <!-- :visible="dialogVisible" -->
    <!-- @update:visible="dialogVisible = $event" -->
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
import { setTimeout } from 'timers'
const cos = new COS({
  SecretId: 'AKIDThiYZi4bhYJqJXIFjrVNL6dxHWYMl8cr',
  SecretKey: 'FK58KNAqGRNL8i30JL9kMpPkuSk3DuLn'
})
export default {
  name: 'UploadImage',
  data() {
    return {
      fileList: [
      ],
      dialogVisible: false,
      dialogImageUrl: '', // 预览的图片地址
      currentUid: '',
      percentage: 0,
      isShowProgress: false
    }
  },

  created() {

  },

  methods: {
    // 点击删除
    onRemove(file, fileList) {
      this.fileList = fileList
    },
    // 自定义上传请求
    onRequest(data) {
      cos.putObject({
        Bucket: 'hrsaas-hz-22-1302962993', /* 必须 */
        Region: 'ap-shanghai', /* 存储桶所在地域，必须字段 */
        StorageClass: 'STANDARD',
        Key: data.file.name, // 名字
        Body: data.file, // 上传文件对象
        onProgress: progressData => {
          this.percentage = progressData.percent * 100
        }
      }, (err, data) => {
        if (err) {
          this.$message.error('上传失败,重新上传')
          this.isShowProgress = false
          this.fileList = this.fileList.filter(item => item.uid !== this.currentUid)
        }
        if (!err && data.statusCode === 200) {
          const url = 'https://' + data.Location
          // console.log(url)
          const index = this.fileList.findIndex(item => {
            return item.uid === this.currentUid
          })
          this.fileList[index].url = url
          // 通过$emit 将数据传递到父组件
          this.$emit('on-success', this.fileList, url)
          setTimeout(() => {
            this.isShowProgress = false
          }, 700)
        }
      })
    },
    // 本地上传成功
    onChange(file, fileList) {
      this.fileList = fileList
    },
    // 点击预览
    handlePictureCardPreview(file) {
      this.dialogVisible = true
      this.dialogImageUrl = file.url
    },
    // 选择图片上传之前
    beforeUpload(file) {
      console.log(file)
      // const type = ['image/gif', 'image/jpeg', 'image/png'] // 限制合法的格式
      const maxSize = 1 * 1024 * 1024 // 限制1MB
      // if (!type.includes(file.type)) {
      //   this.$message.error('亲, 请选择正确的图片')
      //   return false
      // }
      if (file.size >= maxSize) {
        this.$message.error('亲, 请选择1MB以内的图片')
        return false
      }
      this.currentUid = file.uid
      this.isShowProgress = true
      return true
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .display .el-upload--picture-card {
  display: none;
}

</style>
